<template>
	<view class="container">
		<uni-list v-if="showStatus">
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只耳号"
				:rightText="pigPregnancyList.pigCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'calendar-filled'}" title="妊检时间"
				   :rightText="pigPregnancyList.pregnancyTime" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="妊检方式"
				:rightText="pregnancyTestType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="检查结果"
						   :rightText="pregnancyTestResult" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="妊娠原因"
				:rightText="pigPregnancyList.pregnancyReason" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'tune-filled'}" title="妊娠天数"
						   :rightText="pigPregnancyList.pregnancyDays+ '天'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="妊娠鉴定员"
				:rightText="pigPregnancyList.hmyStaff.staffName" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'chat-filled'}" title="备注">
				<template v-slot:footer>
					<input type="textarea" v-model="pigPregnancyList.remark"
						style="font-size: 12px;color: #999999;text-align: right;" />
				</template>
			</uni-list-item>
		</uni-list>
		<view class="button-group" v-if="showStatus">
			<button type="warn" plain="true" @click="pigPregnancyDelete" style="width: 45%;border: #E53F32 solid 1px;">删 除</button>
			<button type="primary" plain="ture" @click="pigPregnancyEdit" style="width: 45%;">修 改</button>
		</view>

		<!-- 修改 -->
		<view class="example" v-if="!showStatus">
		  <uni-forms ref="form" :model="pigPregnancyForm"   :rules="rules" labelWidth="80px">
		    <uni-forms-item label="猪只耳号" name="pigCode" required>
		      <uni-easyinput v-model="pigPregnancyForm.pigCode" placeholder="请输入猪只耳号" />
		    </uni-forms-item>
			  <uni-forms-item label="妊检时间" name="pregnancyTime" required>
				  <uni-datetime-picker type="datetime" v-model="pigPregnancyForm.pregnancyTime"/>
			  </uni-forms-item>
			<uni-forms-item label="妊检方式" name="pregnancyTestType" required>
			  <uni-data-select v-model="pigPregnancyForm.pregnancyTestType" :placeholder="pregnancyTestType"
			  	:localdata="pregnancyTestTypes"
			    ></uni-data-select>
			</uni-forms-item>
			  <uni-forms-item label="妊检结果" name="pregnancyTestType" required>
				  <uni-data-select v-model="pigPregnancyForm.pregnancyTestResult" :placeholder="pregnancyTestResult"
								   :localdata="pregnancyTestResults"
				  ></uni-data-select>
			  </uni-forms-item>


			<uni-forms-item label="妊娠原因" name="pregnancyReason" required>
			  <uni-easyinput v-model="pigPregnancyForm.pregnancyReason" placeholder="请输入妊娠原因" />
			</uni-forms-item>
			  <uni-forms-item label="妊娠天数" name="pregnancyDays" required>
				  <uni-easyinput type="number" v-model="pigPregnancyForm.pregnancyDays" placeholder="请输入妊娠天数(天)" />
			  </uni-forms-item>
			<uni-forms-item label="妊娠鉴定员" name="pregnancyTestManager" required>
			  <uni-data-select v-model="pigPregnancyForm.pregnancyTestManager" :placeholder="pigPregnancyForm.hmyStaff.staffName"
			  	:localdata="staffNameList"
			    ></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="pigPregnancyForm.remark" placeholder="请输入备注" />
				</uni-forms-item>
			  </uni-forms>
			  <view class="edit-button-group">
			  	<button type="primary" @click="back" style="width: 45%;">返 回</button>
			  	<button type="primary" @click="editPigPregnancy('form')" style="width: 45%;">提 交</button>
			  </view>
			</view>
	</view>
</template>

<script>
	import { findAllStaffName,getPigPregnancy, updatePigPregnancy, delPigPregnancy} from "@/api/hmy/pigPregnancy";


	export default {
		data() {
			return {
				showStatus:true,
				pregnancyId: '',
				pigPregnancyList: {
					hmyStaff:{
						staffName:''
					}
				},
				pregnancyTestType: '',
				pigPregnancyForm:'',
				pregnancyTestResult:'',
				pregnancyTestTypes: [{
				  text: ' 外部观察发',
				  value: "1"
				},
				{
				  text: ' 直肠触诊法',
				  value: "2"
				},
				{
					text: ' 超声波诊断法',
							value: "3"
				},
				{
					text: ' 激素诊断法',
							value: "4"
				}],
				pregnancyTestResults: [{
					text: '阴性 (未怀孕)',
					value: "1"
				},{
					text: '阳性(已怀孕)',
					value: "20"
				}],
				pregnancyTestManagers: [{
					text: '威尔维克',
					value: "23"
				},{
					text: '王柏',
					value: "5"
				}],
				rules: {
					pigCode: {
						rules: [{required: true, errorMessage: '猪只耳号不能为空'}]
					},
					pregnancyTestType: {
						rules: [{required: true, errorMessage: '妊检方式不可为空'}]
					},
					pregnancyTestResult: {
				  		rules: [{required: true, errorMessage: '妊检结果不能为空'}]
					},
					pregnancyTestManager: {
						rules: [{required: true, errorMessage: '妊娠鉴定员不能为空'}]
					}
				},
				//所有物料负责人
				staffNameList: [],
			}
		},

		created() {
			findAllStaffName().then(response => {
				var staffList = response.data;
				staffList.forEach(item => {
					this.staffNameList.push({
						'text': item.staffName,
						'value': item.staffId
					})
				})
				//console.log(this.staffNameList);
			})
		},


		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			//console.log(option.id);
			this.pregnancyId = option.id;
			this.getPigPregnancyInfo();
		},
		methods: {
			getPigPregnancyInfo() {
				getPigPregnancy(this.pregnancyId).then(response => {
					this.pigPregnancyForm = response.data;
					this.pigPregnancyList = response.data;
					// 两个字典类型
					if (this.pigPregnancyList.pregnancyTestType == 1) {
						this.pregnancyTestType = '外部观察发'
					} else if(this.pigPregnancyList.pregnancyTestType == 2) {
						this.pregnancyTestType = '直肠触诊法3'
					}
					else if(this.pigPregnancyList.pregnancyTestType == 3) {
						this.pregnancyTestType = '超声波诊断法'
					}
					else if(this.pigPregnancyList.pregnancyTestType == 4) {
						this.pregnancyTestType = '激素诊断法'
					}


					if (this.pigPregnancyList.pregnancyTestResult == 1) {
						this.pregnancyTestResult = '阴性 (未怀孕)'
					} else if(this.pigPregnancyList.pregnancyTestResult == 2) {
						this.pregnancyTestResult = '阳性(已怀孕)'
					}

					if (this.pigPregnancyList.pregnancyTestManager == 23) {
						this.pregnancyTestManager = '威尔维克'
					} else if(this.pigPregnancyList.pregnancyTestManager == 2) {
						this.pregnancyTestManager = '王柏'
					}

					//隐藏加载框
					uni.hideLoading();

				})
			},

			//删除
			pigPregnancyDelete() {
				console.log("delete", this.pigPregnancyList.pregnancyId)
				this.$modal.confirm('是否确认删除妊检记录编号为"' +this.pigPregnancyList.pregnancyId + '"的数据项？').then(() => {
					return delPigPregnancy(this.pigPregnancyList.pregnancyId);
				}).then(response => {
					this.$modal.msgSuccess("删除成功");
					this.$tab.navigateTo('/pages/hmy/pigPregnancy/index');
				}).catch(()=>{

				});
			},

			//修改
			pigPregnancyEdit() {
				console.log("edit", this.pigPregnancyList.pregnancyId)
				this.showStatus = !this.showStatus
			},
			//返回
			back(){
				this.showStatus = !this.showStatus
				this.getPigPregnancy();
			},

			//修改提交
			editPigPregnancy(form) {
			  this.$refs[form].validate().then(res => {
				  updatePigPregnancy(this.pigPregnancyForm).then(response => {
			      this.$modal.msgSuccess("修改成功");
				  setTimeout(()=>{
				  	this.$tab.navigateTo('/pages/hmy/pigPregnancy/index');
				  },1000)

			    })
			  })
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}

	.button-group {
		display: flex;
		justify-content: space-around;
		width:100%;
		position:fixed;
		bottom:5px;

	}

	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	.edit-button-group{
		display: flex;
		justify-content: space-around;
	}
</style>
